package cn.dnf.system.service.impl;

import cn.dnf.basic.service.impl.BaseServiceImpl;
import cn.dnf.system.domain.Menu;
import cn.dnf.system.domain.Role;
import cn.dnf.system.mapper.RoleMapper;
import cn.dnf.system.service.IMenuService;
import cn.dnf.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void save(Role role) {
        //三张表  role(返回自增的id)

        roleMapper.save(role);
        if(role.getPermissions().size()>0){
            //添加  t_role_menu
            roleMapper.saveRoleMenu(role.getId(),role.getMenus());
        }

        if(role.getPermissions().size()>0){
            //添加  t_role_permission
            roleMapper.saveRolePermission(role.getId(),role.getPermissions());
        }


    }

    @Override
    public void update(Role role) {
        // 修改
        roleMapper.update(role);
        //先删除,再添加
        roleMapper.removeRolePermission(role.getId());
        roleMapper.removeRoleMenu(role.getId());

        //添加操作
        if(role.getPermissions().size()>0){
            //添加  t_role_menu
            roleMapper.saveRoleMenu(role.getId(),role.getMenus());
        }

        if(role.getPermissions().size()>0){
            //添加  t_role_permission
            roleMapper.saveRolePermission(role.getId(),role.getPermissions());
        }
    }

    @Override
    public void remove(Long rid) {
        //删除,  先删除关联表,再删除自己
        roleMapper.removeEmployeeRole(rid);
        roleMapper.removeRoleMenu(rid);
        roleMapper.removeRolePermission(rid);
        roleMapper.remove(rid);
    }
}
